<template>
  <view>
    
    
    <view class="tourist">
      <uni-icons type="contact-filled" size="100" color="#c3c3c3"></uni-icons>
      <button type="warn" style="background-color:#c00000;border-radius: 20px;" open-type="getUserInfo" @getuserinfo="onGotUserInfo">一键登录</button>
      <view class="hint">
        登录后尽享更多权益
      </view>
    </view>
    
    
  </view>
</template>

<script>
  import {mapState,mapMutations} from 'vuex'
  
  export default {
    name:"my-login",
    computed:{
      ...mapState('my_user',['token','redirectInfo'])
    },
    
    methods:{
      ...mapMutations('my_user',['updateUserInfo','upToken','upReInfo']),
      
      /*通过getuserinfo获取用户基本信息*/
      onGotUserInfo(e){
        if(e.detail.errMsg=="getUserInfo:fail auth deny") return uni.showMsg('已取消了授权')
        this.updateUserInfo(e.detail.userInfo)
        
        //this.getToken(e.detail)//getToken由于接口不可以，所以需要自己赋值token值
        this.getToken()
      },
      
      getToken(){
        this.upToken("unap56443a378")
        uni.showMsg('登录成功')
        setTimeout(()=>{
          this.navigatorBack()
        },1000)        
      },
      
      navigatorBack(){
        uni.switchTab({
          url:this.redirectInfo.from,
          complete: () => {//回调，成功失败都会执行
            this.upReInfo(null)
          }
        })
      }
      // async getToken(info){
      //   const [err,res] = await uni.login().catch(err=>err)
        
      //   if(err || res.errMsg!="login:ok") return uni.$showError('登录失败!')
      //   const query = {
      //     code:res.code,//通过uni.login()获取code
      //     encryptedData:info.encryptedData,
      //     iv:info.iv,
      //     rawData:JSON.stringify(info.rawData),
      //     signature:info.signature
      //   }
      //   console.log(query.rawData)
      //   const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
      //   if(loginResult.meta.status!=200) return uni.showMsg('!200')        
      //   this.upToken(loginResult.message.token)
      //   uni.showMsg('登录成功')
      // }
      
      
    }
  }
</script>

<style lang="scss">
.tourist{
    text-align: center;
    padding: 100px 5px;
    .hint{
      color: #acacac;
      padding: 10px 0px;
    }
  }
</style>